<script setup>
import { ref } from 'vue'
import { userStore } from '@/stores/loginStore'
import router from '@/router'
const userInfo = userStore()
const loginForm = ref({
  userName: '',
  userPassword: '',
})
// 校验规则
const loginRules = ref({
  userName: [
    { required: true, message: '账号不能为空！', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在3-20位之间！', trigger: 'blur' },
  ],
  userPassword: [
    { required: true, message: '密码不能为空！', trigger: 'blur' },
    { min: 3, max: 60, message: '密码长度在3-60位之间！', trigger: 'blur' },
  ]
})
//登录
// 关联表单
const loginFormRef = ref()
const submit = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      await userInfo.userLogin(loginForm.value)
      // console.log(res);
      if (userInfo.isLogin) {
        router.push('/index')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>
<template>
  <el-row class="login-container">
    <el-col :span="17" class="left-panel">
      <div class="bg-left"></div>
    </el-col>
    <el-col :span="7" class="right-panel">
      <div class="login-form-container">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="120px" class="login-form">
          <h3 class="login-title">欢迎来到WMS-System!👋🏻</h3>
          <el-form-item label="账号:" prop="userName">
            <el-input v-model="loginForm.userName" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码:" prop="userPassword">
            <el-input type="password" v-model="loginForm.userPassword" placeholder="请输入密码" />
          </el-form-item>
          <el-button type="primary" class="login-button" @click="submit">登录</el-button>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
}

.left-panel {
  display: flex;
  align-items: center;
  justify-content: center;
}

.bg-left {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background-color: white;
  background-image: url("../../assets/images/loginLeft.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  background-size: 66% 70%;
}

.right-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.login-form {
  width: 100%;
}

.login-button {
  width: 100%;
  margin-top: 20px;
}
</style>